package com.ac.scum.log.web.model.player.controller;

import com.ac.common.core.exception.BaseException;
import com.ac.common.core.exception.config.ExceptionType;
import com.ac.common.core.result.Result;
import com.ac.scum.log.web.model.player.service.PlayerCustomizePackageService;
import com.ac.scum.log.web.model.player.service.ServerCustomizePackageService;
import com.ac.scum.log.web.utils.user.CurrentUserUtil;
import com.ac.scum.player.bean.ServerCustomizePackage;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.micrometer.common.util.StringUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 自定义套餐
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("server_customize/package")
public class ServerCustomizePackageController {

    private final ServerCustomizePackageService serverCustomizePackageService;
    private final PlayerCustomizePackageService playerCustomizePackageService;

    /**
     * 添加服务器自定义套餐
     */
    @PostMapping("addOrUpdate")
    public Result<Boolean> addCustomizePackage(@RequestBody ServerCustomizePackage serverCustomizePackage) throws BaseException {
        if (serverCustomizePackage.getId() != null) {
            if (serverCustomizePackageService.getById(serverCustomizePackage.getId()) != null) {
                serverCustomizePackage.setModifyTime(null);
                serverCustomizePackage.setCreateTime(null);
                return Result.ok(serverCustomizePackageService.updateById(serverCustomizePackage));
            }
        }
        serverCustomizePackage.setId(null);
        serverCustomizePackage.setServerId(CurrentUserUtil.getCurrentServerId());
        return Result.ok(serverCustomizePackageService.save(serverCustomizePackage));
    }

    @GetMapping("by/name{name}")
    public Result<List<ServerCustomizePackage>> getCustomizePackageByName(@PathVariable String name) throws BaseException {
        LambdaQueryWrapper<ServerCustomizePackage> wrapper = Wrappers.<ServerCustomizePackage>lambdaQuery()
                .eq(ServerCustomizePackage::getServerId, CurrentUserUtil.getCurrentServerId());
        if (StringUtils.isNotBlank(name)) {
            wrapper.like(ServerCustomizePackage::getName, name);
        }
        return Result.ok(serverCustomizePackageService.list(wrapper));
    }

    @GetMapping("by/id{id}")
    public Result<ServerCustomizePackage> getCustomizePackageById(@PathVariable Long id) throws BaseException {
        return Result.ok(serverCustomizePackageService.getById(id));
    }


    /**
     * 查询服务器自定义套餐
     */
    @GetMapping("page/{current}/{size}")
    public Result<Page<ServerCustomizePackage>> serverCustomizePackagePage(@PathVariable("current") Long current, @PathVariable("size") Long size,
                                                                           @RequestParam(defaultValue = "", required = false) String packageName) throws BaseException {
        LambdaQueryWrapper<ServerCustomizePackage> serverCustomizePackageLambdaQueryWrapper = Wrappers.<ServerCustomizePackage>lambdaQuery()
                .eq(ServerCustomizePackage::getServerId, CurrentUserUtil.getCurrentServerId())
                .orderByDesc(ServerCustomizePackage::getCreateTime);
        if (StringUtils.isNotBlank(packageName)) {
            serverCustomizePackageLambdaQueryWrapper.like(ServerCustomizePackage::getName, packageName);
        }
        return Result.ok(serverCustomizePackageService.page(new Page<>(current, size), serverCustomizePackageLambdaQueryWrapper));
    }

    /**
     * 删除服务器自定义套餐
     */
    @DeleteMapping("{id}")
    public Result<Boolean> deleteCustomizePackage(@PathVariable Long id) throws BaseException {
        if (playerCustomizePackageService.findPackageCount(CurrentUserUtil.getCurrentServerId(), id) > 0) {
            throw new BaseException(ExceptionType.Parameter_Error.of("该套餐下还有未结余的玩家套餐，请先将玩家套餐删除"));
        }
        return Result.ok(serverCustomizePackageService.removeById(id));
    }
}
